//
//  FFVoicePlayTextControllerViewController.m
//  voiceReadingNewspapers
//
//  Created by wanglei on 17/1/10.
//  Copyright © 2017年 wanglei. All rights reserved.
//

#import "FFVoicePlayTextControllerViewController.h"

@interface FFVoicePlayTextControllerViewController ()<AVSpeechSynthesizerDelegate>
@property (weak, nonatomic) IBOutlet UITextView *textView;
// 语言数组
@property(nonatomic,strong)NSArray <AVSpeechSynthesisVoice *> *laungeVoices;
// 语音合成器
@property(nonatomic,strong)AVSpeechSynthesizer *synthesizer;

@end

@implementation FFVoicePlayTextControllerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.textView.backgroundColor = [UIColor grayColor];
    self.textView.text = @"电影《三少爷的剑》尽管是徐克和尔冬升联手打造的武侠巨制，但是结果很意外，没有得到武侠迷的追捧，估计二人到死也不知道是怎么死的这么冤枉，因为演员找错了，根本不应该找林更新这种没有武术功底的小生，吴京、吴越、甄子丹都是不二人选，再不济谢霆锋、安志杰也很好的，但是不用，非要看人气，人气都是假的，演员的演技才是真的。小编感慨可惜";
}

// 懒加载
-(NSArray<AVSpeechSynthesisVoice *> *)laungeVoices{
    
    if (_laungeVoices == nil) {
        _laungeVoices = @[
                          // 美式英语
                          [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"],
                          // 英式英语
                          [AVSpeechSynthesisVoice voiceWithLanguage:@"en-GB"],
                          // 中文
                          [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"]
                          ];
        
    }
 return _laungeVoices;
    
}

-(AVSpeechSynthesizer *)synthesizer{
    if (_synthesizer == nil) {
        _synthesizer = [[AVSpeechSynthesizer alloc]init];
        _synthesizer.delegate = self;
        
    }
    return _synthesizer;
}

#pragma mark 语音处理
- (IBAction)voiceBtnClick:(UIButton *)sender {
    
  // 创建一个对话
    AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:self.textView.text];
 // 选择语音发音的类别,如果有中文,一定选择中文
    utterance.voice = self.laungeVoices[2];
  // 播放语音的速录,值越大,速度越快
    utterance.rate = 0.4f;
    //音调  --  为语句指定pitchMultiplier ，可以在播放特定语句时改变声音的音调、pitchMultiplier的允许值一般介于0.5(低音调)和2.0(高音调)之间
    utterance.pitchMultiplier = 0.8f;
    
    //让语音合成器在播放下一句之前有短暂时间的暂停，也可以类似的设置preUtteranceDelay
    utterance.postUtteranceDelay = 0.1f;
    
    if (self.synthesizer.isSpeaking) { //正在语音播放
        
        //立即停止播放语音
        [self.synthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
    }
    else{
        //播放语言
        [self.synthesizer speakUtterance:utterance];
        
    }
    
}

/** 暂停语音播放/继续语音播放 */
- (IBAction)playAndPauseBtnClick:(UIButton *)sender {
    
    if (self.synthesizer.isPaused == YES) { //暂停状态
        //继续播放
        [self.synthesizer continueSpeaking];
    }
    else { //现在在播放
        //立即暂停播放
        [self.synthesizer pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];
    }

}


/**********************AVSpeechSynthesizerDelegate(代理方法)***********************/
#pragma mark - AVSpeechSynthesizerDelegate(代理方法)

- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance {
    NSLog(@"开始播放语音的时候调用");
}


- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance {
    NSLog(@"语音播放结束的时候调用");
}


- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance *)utterance {
    NSLog(@"暂停语音播放的时候调用");
}


- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance *)utterance {
    NSLog(@"继续播放语音的时候调用");
}


- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance *)utterance {
    NSLog(@"取消语音播放的时候调用");
}


- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance {
    
    /** 将要播放的语音文字 */
    NSString *willSpeakRangeOfSpeechString = [utterance.speechString substringWithRange:characterRange];
    
    NSLog(@"即将播放的语音文字:%@",willSpeakRangeOfSpeechString);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
